package com.xiangwei.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;



import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiangwei.dao.ExtraColumnMapper;
import com.xiangwei.dao.ImageMapper;
import com.xiangwei.dao.ProductMapper;
import com.xiangwei.dao.VendorMapper;
import com.xiangwei.entity.ExtraColumn;
import com.xiangwei.entity.Image;
import com.xiangwei.entity.Product;
import com.xiangwei.entity.Vendor;
import com.xiangwei.service.IProductService;
import com.xiangwei.util.Common;
import com.xiangwei.util.Const;
import com.xiangwei.view.ProductView;

@Service
public class ProductServiceImpl implements IProductService{
	private static final Logger logger = LoggerFactory
			.getLogger(VendorServiceImpl.class);
	
	@Resource
	private ProductMapper productMapper;
	
	@Resource
	private VendorMapper vendorMapper;
	
	@Resource
    private ExtraColumnMapper extraColumnMapper;
	
    @Resource
    private ImageMapper imageMapper;
	
	@Transactional
	public void deleteByPrimaryKey(Integer productId,HttpServletRequest request) {
		ExtraColumn e = new ExtraColumn();
		e.setPid(productId);
		e.settType(Short.parseShort("1"));////0表示厂商，1表示产品
		extraColumnMapper.deleteByPIdAndType(e);
		
		Image img = new Image();
		img.setPid(productId);
		img.setType(Short.parseShort("1"));//0表示厂商，1表示产品
		imageMapper.delImagesByPidAndType(img);
		
		Product p = productMapper.selectByPrimaryKey(productId);
		productMapper.deleteByPrimaryKey(productId);
		if(Common.isNullOrEmpty(p) || !Common.isEmptyString(p.getVideo())){
			return ;
		}

//		String filePath = p.getVideo();
//		System.out.println(filePath);
//		File file = new File(filePath);
//		if(file.exists()){
//			System.out.println("file.exists()="+file.exists());
//			boolean flg = file.delete();
//			System.out.println(flg);
//			if(flg){
//				logger.info("视频文件删除成功");				
//			}else{
//				logger.info("视频文件删除失败");
//			}
//		}
	}

	@Transactional
	public void insert(Product record,String[] cName,String[] cValue) {				
		
		if(Common.isNullOrEmpty(record)){
			logger.error("传入参数为空");
			return;
		}
		Map<Object,Object> map = new HashMap<Object, Object>();
		map.put("pName", record.getPName());
		map.put("vId", record.getVId());

		int count = productMapper.findProductNameCount(map);
		if(count != 0){
			logger.error("产品名称重复");
			return;
		}
		productMapper.insert(record);
		Product p = productMapper.selectByNameAndVid(map);
		if(Common.isNullOrEmpty(cName) || cName.length == 0){
			return;
		}
		for(int i=0;i<cName.length;i++){
			if(!Common.isEmptyString(cName[i]) || !Common.isEmptyString(cValue[i])){
				continue;
			}
			ExtraColumn e = new  ExtraColumn();
			e.setPid(p.getId());
			e.settType(Short.parseShort("1"));//0表示厂商，1表示产品
			e.setcName(cName[i]);
			e.setcValue(cValue[i]);
			extraColumnMapper.insertSelective(e);
		}
		
		
		
		
	}

	
	public Product selectByPrimaryKey(Integer productId) {
		return productMapper.selectByPrimaryKey(productId);
	}

	@Transactional
	public void updateByPrimaryKey(Product record,String[] cName,String[] cValue,
			String[] eId) {
		
		if(!Common.isNullOrEmpty(cName) && cName.length > 0){
			for(int i=0;i<cName.length;i++){
				if(!Common.isEmptyString(cName[i]) || !Common.isEmptyString(cValue[i])){
					continue;
				}
				ExtraColumn e = new  ExtraColumn();
				e.setPid(record.getId());
				e.settType(Short.parseShort("1"));//0表示厂商，1表示产品
				e.setcName(cName[i]);
				e.setcValue(cValue[i]);
				if(Integer.parseInt(eId[i]) == 0){
					extraColumnMapper.insertSelective(e);
				}else{
					e.setId(Integer.parseInt(eId[i]));
					extraColumnMapper.updateByPrimaryKeySelective(e);				
				}
			}			
		}
		Product p = productMapper.selectByPrimaryKey(record.getId());
		if(productMapper.updateByPrimaryKey(record)>0){
			logger.info("产品信息更新成功");
		};
		
//		if(Common.isNullOrEmpty(p)){
//			return ;
//		}
//		String filePath = p.getVideo();
//		if(Common.isEmptyString(filePath)){
//			File file = new File(filePath);
//			if(file.exists()){
//				if(file.delete()){
//					logger.info("视频文件删除成功");				
//				}else{
//					logger.info("视频文件删除失败");
//				}
//			}			
//		}
		
	}

	@Override
	public Product getProduct(Integer productId) {
		return productMapper.getProduct(productId);
	}

	@Override
	public List<Product> getAll(Integer start, Integer pageSize) {
		return productMapper.getAll(start, pageSize);
	}

	/**
	 * 获取总数
	 */
	@Override
	public Integer getCount() {
		return productMapper.getCount();
	}

	/**
	 * 根据公司名称和产品名称查询产品信息
	 */
	@Override
	public List<Vendor> selectByVendorName(String vendorName) {
		return productMapper.selectByVendorName(vendorName);
	}

	/**
	 * 检查产品名称是否重复
	 * @param productName
	 * @return
	 */
	@Override
	public int checkProductName(String productName,String vId,String id) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		if(!Common.isEmptyString(productName) || !Common.isEmptyString(vId) ){
			logger.error("传入参数为空");
			return -1;
		}		
		map.put("pName", productName);
		map.put("vId", vId);
		int count = productMapper.findProductNameCount(map);//按照厂商id和产品名称查找
		if(count == 0 ){
			return 0;
		}else if(Common.isEmptyString(id) && Integer.parseInt(id) > 0 && count == 1){
			return 0;
		}
		return -1;
	}

	@Override
	public List<Product> getProductList(Integer vendorId) {
		return productMapper.getProductList(vendorId);
	}

	@Override
	public List<Product> selectProductByVendorName(String productName) {
		return productMapper.selectProductByVendorName(productName);
	}

	@Override
	public int updateByPrimaryKeySelective(Product record) {
		return productMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public ProductView selectProductViewById(String id) {
		ProductView pv = new ProductView();
		if(!Common.isEmptyString(id)){
			logger.error("传入参数为空");
			return pv;
		}
		Product product = productMapper.selectByPrimaryKey(Integer.parseInt(id));
		ExtraColumn e = new ExtraColumn();
		e.setPid(Integer.parseInt(id));
		e.settType(Short.parseShort("1"));//厂商信息0  产品信息1
		List<ExtraColumn> ecList = extraColumnMapper.selectByPIdAndType(e);
		pv.setExtraColumnList(ecList);
		pv.setProduct(product);
		return pv;
	}

	@Override
	public int findProductCount(String vId) {
		if(!Common.isEmptyString(vId)){
			logger.error("传入参数为空");
			return -1;
		}
		return productMapper.findProductCount(Integer.parseInt(vId));
	}

	@Override
	public List<Product> getProductAllList() {
		return productMapper.getAllProduct();
	}

	@Override
	public Product getMaxIdProduct() {
		return productMapper.getMaxIdProduct();
	}

	
}
